﻿#region zh-CHS 2006 - 2007 DemoSoft 团队 | en 2006 - 2007 DemoSoft Team

//     NOTES
// ---------------
//
// This file is a part of the G.O.S.E(Game Online Server Engine) for .NET.
//
//                 2006-2007 DemoSoft Team
//
//
// First Version : by H.Q.Cai - mailto:caihuanqing@hotmail.com

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published
 *   by the Free Software Foundation; either version 2.1 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

#region zh-CHS 包含名字空间 | en Include namespace
using System;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;
using System.Collections.Generic;
using Demo_G.O.S.E.ServerEngine.Util;
using Demo_G.O.S.E.ServerEngine.Common;
#endregion

namespace Demo_G.O.S.E.ServerEngine.Mail
{
    /// <summary>
    /// 
    /// </summary>
    public class BaseMailManager
    {
        #region zh-CHS 共有属性 | en Public Properties

        #region zh-CHS BaseMailBox列表 属性 | en Public Properties
        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private SafeDictionary<long, BaseMailBox> m_MailBoxs = new SafeDictionary<long, BaseMailBox>();
        #endregion
        /// <summary>
        /// 
        /// </summary>
        [MultiThreadedWarningAttribute( "(邮件信息)当前的数组是列表临时产生的,不能保存数组用于以后操作:警告!" )]
        public BaseMailBox[] ToArray()
        {
            return m_MailBoxs.ToArray();
        }

        #region zh-CHS 共有方法 | en Public Methods

        /// <summary>
        /// 添加道具
        /// </summary>
        /// <param name="item"></param>
        public bool AddMailBox( long iMailBoxSerial, BaseMailBox baseMailBox )
        {
            if ( baseMailBox == null )
            {
                Debug.WriteLine( "BaseMailManager.AddMailBox(...) - baseMailBox == null error!" );

                return false;
            }

            EventHandler<BeforeAddMailBoxCallEventArgs> tempBeforeEventArgs = m_ThreadEventBeforeAddMailBoxCall;
            if ( tempBeforeEventArgs != null )
            {
                BeforeAddMailBoxCallEventArgs eventArgs = new BeforeAddMailBoxCallEventArgs( baseMailBox, this );
                tempBeforeEventArgs( this, eventArgs );

                if ( eventArgs.IsCancel == true )
                    return false;
            }

            m_MailBoxs.Add( iMailBoxSerial, baseMailBox );

            EventHandler<AfterAddMailBoxCallEventArgs> tempAfterEventArgs = m_ThreadEventAfterAddMailBoxCall;
            if ( tempAfterEventArgs != null )
            {
                AfterAddMailBoxCallEventArgs eventArgs = new AfterAddMailBoxCallEventArgs( baseMailBox, this );
                tempAfterEventArgs( this, eventArgs );
            }

            return true;
        }

        /// <summary>
        /// 移出道具
        /// </summary>
        /// <param name="item"></param>
        public bool RemoveMailBox( long iMailBoxSerial )
        {
            BaseMailBox baseMailBox = FindMailBoxOnSerial( iMailBoxSerial );
            if ( baseMailBox == null )
                return false;

            EventHandler<BeforeRemoveMailBoxCallEventArgs> tempBeforeEventArgs = m_ThreadEventBeforeRemoveMailBoxCall;
            if ( tempBeforeEventArgs != null )
            {
                BeforeRemoveMailBoxCallEventArgs eventArgs = new BeforeRemoveMailBoxCallEventArgs( baseMailBox, this );
                tempBeforeEventArgs( this, eventArgs );

                if ( eventArgs.IsCancel == true )
                    return false;
            }

            m_MailBoxs.Remove( iMailBoxSerial );

            EventHandler<AfterRemoveMailBoxCallEventArgs> tempAfterEventArgs = m_ThreadEventAfterRemoveMailBoxCall;
            if ( tempAfterEventArgs != null )
            {
                AfterRemoveMailBoxCallEventArgs eventArgs = new AfterRemoveMailBoxCallEventArgs( baseMailBox, this );
                tempAfterEventArgs( this, eventArgs );
            }

            return true;
        }

        /// <summary>
        /// 在身上找道具
        /// </summary>
        /// <param name="layer"></param>
        /// <returns></returns>
        public BaseMailBox FindMailBoxOnSerial( long iMailBoxSerial )
        {
            return m_MailBoxs.GetValue( iMailBoxSerial );
        }

        #endregion

        #endregion

        #endregion

        #region zh-CHS 共有事件 | en Public Event

        #region zh-CHS AddMailBoxCall事件 | en Public Event
        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private EventHandler<BeforeAddMailBoxCallEventArgs> m_ThreadEventBeforeAddMailBoxCall;
        /// <summary>
        /// 
        /// </summary>
        private ReaderWriterLockSlim m_LockThreadEventBeforeAddMailBoxCall = new ReaderWriterLockSlim();
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public event EventHandler<BeforeAddMailBoxCallEventArgs> ThreadBeforeAddMailBoxCall
        {
            add
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventBeforeAddMailBoxCall );
                {
                    m_ThreadEventBeforeAddMailBoxCall += value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventBeforeAddMailBoxCall );
            }
            remove
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventBeforeAddMailBoxCall );
                {
                    m_ThreadEventBeforeAddMailBoxCall -= value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventBeforeAddMailBoxCall );
            }
        }

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private EventHandler<AfterAddMailBoxCallEventArgs> m_ThreadEventAfterAddMailBoxCall;
        /// <summary>
        /// 
        /// </summary>
        private ReaderWriterLockSlim m_LockThreadEventAfterAddMailBoxCall = new ReaderWriterLockSlim();
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public event EventHandler<AfterAddMailBoxCallEventArgs> ThreadAfterAddMailBoxCall
        {
            add
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventAfterAddMailBoxCall );
                {
                    m_ThreadEventAfterAddMailBoxCall += value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventAfterAddMailBoxCall );
            }
            remove
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventAfterAddMailBoxCall );
                {
                    m_ThreadEventAfterAddMailBoxCall -= value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventAfterAddMailBoxCall );
            }
        }
        #endregion

        #region zh-CHS RemoveMailBoxCall事件 | en Public Event
        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private EventHandler<BeforeRemoveMailBoxCallEventArgs> m_ThreadEventBeforeRemoveMailBoxCall;
        /// <summary>
        /// 
        /// </summary>
        private ReaderWriterLockSlim m_LockThreadEventBeforeRemoveMailBoxCall = new ReaderWriterLockSlim();
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public event EventHandler<BeforeRemoveMailBoxCallEventArgs> ThreadBeforeRemoveMailBoxCall
        {
            add
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventBeforeRemoveMailBoxCall );
                {
                    m_ThreadEventBeforeRemoveMailBoxCall += value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventBeforeRemoveMailBoxCall );
            }
            remove
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventBeforeRemoveMailBoxCall );
                {
                    m_ThreadEventBeforeRemoveMailBoxCall -= value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventBeforeRemoveMailBoxCall );
            }
        }

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private EventHandler<AfterRemoveMailBoxCallEventArgs> m_ThreadEventAfterRemoveMailBoxCall;
        /// <summary>
        /// 
        /// </summary>
        private ReaderWriterLockSlim m_LockThreadEventAfterRemoveMailBoxCall = new ReaderWriterLockSlim();
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public event EventHandler<AfterRemoveMailBoxCallEventArgs> ThreadAfterRemoveMailBoxCall
        {
            add
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventAfterRemoveMailBoxCall );
                {
                    m_ThreadEventAfterRemoveMailBoxCall += value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventAfterRemoveMailBoxCall );
            }
            remove
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventAfterRemoveMailBoxCall );
                {
                    m_ThreadEventAfterRemoveMailBoxCall -= value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventAfterRemoveMailBoxCall );
            }
        }
        #endregion

        #endregion
    }
}
#endregion